﻿#coding=utf-8

import sys
 


[LOG_ERR,LOG_DBG,LOG_INF,LOG_WRN,LOG_TODO] = [i for i in range(5)]
[ERR,DBG,INF,WRN,TODO,TRC] = [i for i in range(6)]
strLvl = ['Err', 'Dbg', 'Inf', 'Wrn','ToDO','Trc']
def log(level, fmt, param=None):
    #print type(param)
    str = '---------[%s]---[%s:%s:%d]----------\n'%(strLvl[level],
                            sys._getframe().f_back.f_back.f_code.co_filename,
                            sys._getframe().f_back.f_back.f_code.co_name,
                            sys._getframe().f_back.f_back.f_lineno)
    str += fmt
    if param is not None:
        if(type(param) is list):         
            str =str %tuple(param)
        else:
            str =str %param
    print str
    

##class logger:
##    @staticmethod
##    def error(fmt, param=None):
##        '''错误信息，发布版中记录需要修复的问题'''
##        log(ERR, fmt,param)
##    
##    @staticmethod
##    def info(fmt, param=None):
##        '''执行过程信息，发布版中记录操作日志'''
##        log(INF, fmt,param)
##        
##    @staticmethod
##    def trace(fmt, param=None):
##        '''跟踪执行流程，发布版中无动作'''
##        log(TRC, fmt,param)
##    
##    @staticmethod
##    def debug(fmt, param=None):
##        '''针对某一处代码调试，发布版中无动作'''
##        log(DBG, fmt,param)
##    
##    @staticmethod
##    def todo(fmt, param=None):
##        '''标记待续处理代码'''
##        log(TODO, fmt,param)

logger = None
def initLogger():
    import os
    import logging  
    logging.basicConfig(filename = os.path.join(os.getcwd(), 'log.txt'), 
                        level = logging.NOTSET,
                        format = '%(asctime)s - %(levelname)s: %(message)s')
    
    global logger
    logger = logging.getLogger()  
    logger.info(u'软件启动')

def getLogger():
    global logger
    if (logger is None):
        initLogger()
        
    return logger
    
def writeLog():
    pass
    
class MyLogger:
    logger = None
    
    @classmethod
    def debug(cls, fmt, param=None):
        if (param is None):
            cls.logger.debug(fmt)
        else:
            cls.logger.debug(fmt,param)
     
def test_log():
    log(1, 'woshi')
    log(1, 'woshi%s', 'lwg')
    log(1, 'woshi%s%d', ('lwg',100))
    log(1, 'woshi%s%d', ['lwg',100])
    logger.error('woshi')
    logger.info('woshi%s', 'lwg')
    logger.warning('woshi%s%d', ('lwg',100))
    logger.debug('woshi%s%d', ['lwg',100])
    logger.critical('woshi%s%d', ['lwg',100])
    
#test_log()

def getLogFile():
    import os
    import datetime
    
    name = str(datetime.datetime.now())[:16] +'.txt'
    name = name.replace(' ', '_')
    name = name.replace(':', '_')
    name = name.replace('-', '_')
        
    fileName = u'\\日志\\log_' + name
    #file = os.path.join(os.getcwd(), fileName)
    file = os.getcwd() + fileName
    #print file
    return file
    
class rdrStdout(object):
    @staticmethod
    def start():
        import sys  
          
        stdout = sys.stdout  
        sys.stdout = open(getLogFile(),'w')